﻿package frEngine.animateControler.uvControler
{
    import baseEngine.core.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.animateControler.*;
    import frEngine.animateControler.keyframe.*;
    import frEngine.animateControler.visible.*;
    import frEngine.event.*;
    import frEngine.shader.*;

    public class FrUvStepControler extends MeshAnimateBase
    {
        private var _playSpeed:int = 1;
        private var _u:Number = 1;
        private var _v:Number = 1;
        private var _totoalNum:int = 1;
        private var _playMode:int = 0;
        private var _startIndex:int = 0;
        private var _useRandom:Boolean = false;
        private var startMap:Dictionary;

        public function FrUvStepControler()
        {
            this.startMap = new Dictionary(false);
            return;
        }// end function

        public function init(param1:int, param2:int, param3:int, param4:int, param5:int = 0, param6:int = 0, param7:Boolean = false) : void
        {
            this._playSpeed = param4;
            this._u = param1;
            this._v = param2;
            this._totoalNum = param3;
            this._playMode = param5;
            targetMesh.setUvScaleBase(1 / this._u, 1 / this._v);
            this._startIndex = param6;
            this._useRandom = param7;
            if (this._useRandom)
            {
                if (this._startIndex == 0)
                {
                    this._startIndex = Math.floor(Math.random() * this._totoalNum);
                }
                else
                {
                    this._startIndex = Math.floor(Math.random() * this._startIndex);
                }
            }
            return;
        }// end function

        override public function get type() : int
        {
            return AnimateControlerType.UvStepControler;
        }// end function

        override public function set targetObject3d(param1:Pivot3D) : void
        {
            var _loc_2:VisibleControler = null;
            if (this.targetMesh)
            {
                _loc_2 = targetMesh.visibleControler;
                FrEventDispatcher.instance.proxyRemoveEventListener(_loc_2, Event.CHANGE, this.visibleChangeHander);
            }
            super.targetObject3d = param1;
            if (targetMesh)
            {
                _loc_2 = targetMesh.getAnimateControlerInstance(AnimateControlerType.VisibleContoler) as VisibleControler;
                FrEventDispatcher.instance.proxyAddEventListener(_loc_2, Event.CHANGE, this.visibleChangeHander);
            }
            return;
        }// end function

        private function visibleChangeHander(event:Event) : void
        {
            this.startMap = new Dictionary(false);
            return;
        }// end function

        override public function toUpdateAnimate(param1:Boolean = false) : void
        {
            var _loc_2:* = targetMesh.visibleControler.currentFrame;
            if (this.startMap[_loc_2] == null)
            {
                this.startMap[_loc_2] = targetMesh.visibleControler.getStartVisibleFrame();
            }
            var _loc_3:* = this.startMap[_loc_2];
            var _loc_4:* = targetMesh.timerContorler.curFrame - _loc_3;
            var _loc_5:* = int(_loc_4 / this._playSpeed) + this._startIndex;
            if (this._playMode == 1)
            {
                _loc_5 = _loc_5 % this._totoalNum;
            }
            else
            {
                _loc_5 = _loc_5 >= this._totoalNum ? ((this._totoalNum - 1)) : (_loc_5);
            }
            var _loc_6:* = int(_loc_5 % this._u) / this._u;
            var _loc_7:* = int(_loc_5 / this._u) / this._v;
            targetMesh.setUvOffsetBase(_loc_6, _loc_7);
            return;
        }// end function

        override protected function setSurfaceHander(event:Event) : void
        {
            super.setSurfaceHander(event);
            this.reBuilderHander(null);
            FrEventDispatcher.instance.proxyAddEventListener(targetMesh.material, Engine3dEventName.MATERIAL_REBUILDER, this.reBuilderHander);
            return;
        }// end function

        private function reBuilderHander(event:Event) : void
        {
            targetMesh.setUvScaleBase(1 / this._u, 1 / this._v);
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            FrEventDispatcher.instance.proxyRemoveEventListener(targetMesh.visibleControler, Event.CHANGE, this.visibleChangeHander);
            FrEventDispatcher.instance.proxyRemoveEventListener(targetMesh.material, Engine3dEventName.MATERIAL_REBUILDER, this.reBuilderHander);
            var _loc_2:* = targetMesh.material.materialParams;
            targetMesh.setUvOffsetBase(_loc_2.UVoffsetBase.x, _loc_2.UVoffsetBase.y);
            targetMesh.setUvScaleBase(_loc_2.uvScaleBasePoint.x, _loc_2.uvScaleBasePoint.y);
            this.startMap = new Dictionary(false);
            super.dispose();
            return;
        }// end function

    }
}
